package wanbang.dao.Imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import wanbang.dao.CartDao;
import wanbang.entities.Cart;
@Repository
public class CartDaoImp implements CartDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public Integer selectCountByBookId(Integer bookId,Integer userId) {
		// TODO Auto-generated method stub
		String sql = " select count(*) from cart where book_id = ? and user_id = ?";
		Integer count = this.jdbcTemplate.queryForObject(sql, Integer.class, bookId,userId);
		return count;
	}

	@Override
	public void update(Integer bookId,Integer userId) {
		// TODO Auto-generated method stub
		String sql = "update cart set cart_number = cart_number+1 where book_id = ? and user_id = ?";
		this.jdbcTemplate.update(sql, bookId,userId);	
	}

	@Override
	public void inertInto(Integer bookId,Integer userId) {
		// TODO Auto-generated method stub
		String sql = "insert into cart(book_id,cart_number,user_Id) values(?,?,?)";
		this.jdbcTemplate.update(sql,bookId,1,userId);
	}

	@Override
	public List<Cart> selectAllCart(Integer userId) {
		// TODO Auto-generated method stub
		String sql = " select book_id ,cart_number from cart where user_id = ?";
		RowMapper<Cart> rowMapper = new BeanPropertyRowMapper<>(Cart.class);
		List<Cart> carts = jdbcTemplate.query(sql, rowMapper, userId);
		return carts;
	}

	@Override
	public void deleteCart(Integer userId, Integer bookId) {
		// TODO Auto-generated method stub
		String sql = "delete from cart where user_id = ? and book_id = ?";
		this.jdbcTemplate.update(sql, userId,bookId);
	}

}
